package com.liao.work.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.liao.work.R;
import com.liao.work.util.SetActionBarTitle;

public class CalculationActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private static final int ADD = 1;
    private static final int MINUS = 2;
    private static final int MULTIPLY = 3;
    private static final int DIVIDED = 4;

    private EditText et_number1;
    private EditText et_number2;
    private TextView tv_result1;
    private TextView tv_result2;
    private TextView tv_result3;
    private TextView tv_result4;
    private CheckBox cb_add;
    private CheckBox cb_minus;
    private CheckBox cb_multiply;
    private CheckBox cb_divided;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculation);
        initView();
        SetActionBarTitle.setTitle(getSupportActionBar(), false, "计算器");
    }

    private void initView() {
        et_number1 = (EditText) findViewById(R.id.et_number1);
        et_number2 = (EditText) findViewById(R.id.et_number2);
        tv_result1 = (TextView) findViewById(R.id.tv_result1);
        tv_result2 = (TextView) findViewById(R.id.tv_result2);
        tv_result3 = (TextView) findViewById(R.id.tv_result3);
        tv_result4 = (TextView) findViewById(R.id.tv_result4);
        cb_add = (CheckBox) findViewById(R.id.cb_add);
        cb_minus = (CheckBox) findViewById(R.id.cb_minus);
        cb_multiply = (CheckBox) findViewById(R.id.cb_multiply);
        cb_divided = (CheckBox) findViewById(R.id.cb_divided);

        cb_add.setOnCheckedChangeListener(this);
        cb_divided.setOnCheckedChangeListener(this);
        cb_minus.setOnCheckedChangeListener(this);
        cb_multiply.setOnCheckedChangeListener(this);
    }

    private void submit(int type) {
        // validate
        String number1 = et_number1.getText().toString().trim();
        if (TextUtils.isEmpty(number1)) {
            Toast.makeText(this, "number1不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String number2 = et_number2.getText().toString().trim();
        if (TextUtils.isEmpty(number2)) {
            Toast.makeText(this, "number2不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            int i = Integer.parseInt(number1);
            int i1 = Integer.parseInt(number2);
            String s;
            switch (type) {
                case ADD:
                    s = "" + i + " + " + i1 + "=" + (i + i1);
                    tv_result1.setText(s);
                    break;
                case MINUS:
                    s = "" + i + " - " + i1 + "=" + (i - i1);
                    tv_result2.setText(s);
                    break;
                case MULTIPLY:
                    s = "" + i + " * " + i1 + "=" + (i * i1);
                    tv_result3.setText(s);
                    break;
                case DIVIDED:
                    s = "" + i + " / " + i1 + "=" + (i / i1);
                    tv_result4.setText(s);
                    break;
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            Toast.makeText(this, "请输入整数", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.cb_add:
                if (isChecked) {
                    submit(ADD);
                    tv_result1.setVisibility(View.VISIBLE);
                } else {
                    tv_result1.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.cb_minus:
                if (isChecked) {
                    submit(MINUS);
                    tv_result2.setVisibility(View.VISIBLE);
                } else {
                    tv_result2.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.cb_multiply:
                if (isChecked) {
                    submit(MULTIPLY);
                    tv_result3.setVisibility(View.VISIBLE);
                } else {
                    tv_result3.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.cb_divided:
                if (isChecked) {
                    submit(DIVIDED);
                    tv_result4.setVisibility(View.VISIBLE);
                } else {
                    tv_result4.setVisibility(View.INVISIBLE);
                }
                break;
        }
    }
}
